package com.syaccp.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.syaccp.mapper.TMessageMapper;
import com.syaccp.model.TMessage;
import com.syaccp.service.TMessageService;
import com.syaccp.service.impl.WebSocket;
import com.syaccp.utils.Result;
import com.syaccp.vo.TMessageVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.SQLException;
import java.util.List;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author syaccp
 * @since 2023-07-15
 */
@RestController
@RequestMapping("/api/message")
@Api(tags = "消息通知")
public class TMessageController {
    @Autowired
    private TMessageService service;

    @Autowired
    private TMessageMapper mapper;

    @Autowired
    private WebSocket webSocket;

    @ApiOperation("根据接收者id分页查询")
    @GetMapping("/selectPage")
    public Result getMessagePageByReceiverId(TMessageVo tMessageVo) throws SQLException {
        Integer index= tMessageVo.getIndex();
            PageInfo<TMessageVo> messageByReceiverId = service.getMessagePageByReceiverId(index, tMessageVo);
            return  Result.success(messageByReceiverId);
    }

    @ApiOperation("查询已读或未读")
    @GetMapping("/selectIsRead")
    public Result getNotRead(TMessageVo tMessageVo) throws SQLException {
        Integer index= tMessageVo.getIndex();
        PageInfo<TMessageVo> messageByReceiverId = service.getNotRead(index, tMessageVo);
        System.out.println("messageByReceiverId = " + messageByReceiverId);
        return  Result.success(messageByReceiverId);
    }





    @ApiOperation("修改消息")
    @PutMapping("update")
    public Result update(@RequestBody TMessage tMessage){
        boolean is_success = service.updateById(tMessage);
        if (is_success){
            return Result.success();
        }else {
            return Result.fail();
        }
    }
    @ApiOperation("根据id删除")
    @DeleteMapping("remove/{message_id}/{receiver_id}")
    public Result remove(@PathVariable Integer message_id,
                         @PathVariable Integer receiver_id) throws SQLException {
        boolean b = service.delMessage(message_id);
        if (b){
            service.delMessageReceiver(message_id,receiver_id);
            return Result.success();
        }
        return Result.success();
    }

    @ApiOperation("修改消息为已读")
    @PutMapping("updateIsRead")
    public Result updateIsRead(@RequestBody TMessageVo tMessageVo) throws SQLException {
        tMessageVo.setMessageId(tMessageVo.getId());
        boolean is_success = service.updateIsRead(tMessageVo);
        if (is_success){
            return Result.success();
        }else {
            return Result.fail();
        }
    }
    @ApiOperation("查看未读消息")
    @GetMapping("selectNotReadMessage")
    public Result selectNotReadMessage(TMessageVo tMessageVo) throws SQLException {

        List<TMessageVo> tMessageVos = service.selectNotReadMessage(tMessageVo);
        return Result.success(tMessageVos);
    }

    @ApiOperation("一键已读")
    @PutMapping("allRead")
    public Result allRead(@RequestBody TMessageVo tMessageVo) throws SQLException {
        tMessageVo.setMessageId(tMessageVo.getId());
        boolean is_success = service.allRead(tMessageVo);
        if (is_success){
            return Result.success();
        }else {
            return Result.fail();
        }
    }
    @ApiOperation("发送公告")
    @PostMapping("sendMessage")
    public Result sendMessage(@RequestBody TMessageVo tMessageVo) throws SQLException {
        QueryWrapper w=new QueryWrapper();
        w.eq("title",tMessageVo.getTitle());
        w.eq("content",tMessageVo.getContent());
        TMessage tMessage1 = mapper.selectOne(w);
        if (tMessage1!=null){
            tMessageVo.setMessageId(tMessage1.getId());
            int i = service.insertMessageReceiver(tMessageVo);
            webSocket.sendMessage(tMessageVo.getContent());
            return Result.success();
        }

        int insert = mapper.insert(tMessageVo);
        TMessage tMessage = mapper.selectOne(w);
        tMessageVo.setMessageId(tMessage.getId());
        if (insert>0){
            int i = service.insertMessageReceiver(tMessageVo);
            if (i>0){
                webSocket.sendMessage(tMessageVo.getContent());
            }
        }
        return Result.success();
    }
}

